package com.qs.service;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import com.infomanage.authority.AuthorityBean;
import com.infomanage.bean.FieldBean;
import com.infomanage.bean.PaginationBean;
import com.infomanage.bean.SearchBean;
import com.infomanage.po.User;
import com.infomanage.util.Consts;
import com.infomanage.util.StringUtil;
import com.jiuqi.util.ReturnObject;
import com.jiuqi.util.StringUtils;
import com.qs.dao.QsRecHandoverDAO;
import com.qs.enums.PeriodOption;
import com.qs.po.QsRecHandover;
import com.qs.util.Period;
import com.qs.util.PeriodUtil;
import com.qs.vo.QsRecHandoverItem;
import com.infomanage.enums.RoleType;


public class RecordHandoverService {

	public static QsRecHandoverItem load(HttpServletRequest request,
			HttpServletResponse response, JspWriter out) {
		try {
			request.setCharacterEncoding("UTF-8");
			String itemId = request.getParameter("itemId");
			if (StringUtils.isEmpty(itemId))
				return null;
			QsRecHandoverDAO dao = new QsRecHandoverDAO();
			return dao.find(Integer.parseInt(itemId));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static List<QsRecHandoverItem> loadList(
			HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		try {
			request.setCharacterEncoding("UTF-8");
			QsRecHandoverDAO dao = new QsRecHandoverDAO();
			List<QsRecHandoverItem> list = dao.findAll();
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static ReturnObject save(HttpServletRequest request,
			HttpServletResponse response, JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			QsRecHandover bean = new QsRecHandover();
			bean.setDeptId(Integer.parseInt(request.getParameter("deptId")));
			bean.setDate(request.getParameter("date"));
			bean.setHandoverCommander(request.getParameter("handoverCommander"));
			bean.setHandoverDetail(request.getParameter("handoverDetail"));
			bean.setRemark(request.getParameter("remark"));
			bean.setTakeoverCommander(request.getParameter("takeoverCommander"));
			bean.setTakeoverDetail(request.getParameter("takeoverDetail"));
			QsRecHandoverDAO dao = new QsRecHandoverDAO();
			dao.add(bean);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(StringUtil.filterIllegalchar(e.getMessage()));
		}
		return ret;
	}

	public static ReturnObject modify(
			HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			int itemId = Integer.parseInt(request.getParameter("itemId"));
			QsRecHandoverDAO dao = new QsRecHandoverDAO();
			QsRecHandover bean = dao.find(QsRecHandover.class, itemId);
			bean.setDeptId(Integer.parseInt(request.getParameter("deptId")));
			bean.setDate(request.getParameter("date"));
			bean.setHandoverCommander(request.getParameter("handoverCommander"));
			bean.setHandoverDetail(request.getParameter("handoverDetail"));
			bean.setRemark(request.getParameter("remark"));
			bean.setTakeoverCommander(request.getParameter("takeoverCommander"));
			bean.setTakeoverDetail(request.getParameter("takeoverDetail"));
			dao.modify(bean);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(StringUtil.filterIllegalchar(e.getMessage()));
		}
		return ret;
	}

	public static ReturnObject remove(
			HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String itemId = request.getParameter("itemId");
			QsRecHandoverDAO dao = new QsRecHandoverDAO();
			dao.remove(QsRecHandover.class, Integer.parseInt(itemId));
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}

	public static List<QsRecHandoverItem> search(HttpServletRequest request,
			HttpServletResponse response, JspWriter out) {
		try {
			request.setCharacterEncoding("UTF-8");
			QsRecHandoverDAO dao = new QsRecHandoverDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			// 设置分页
			String currPageNum = request.getParameter("currPageNum");
			PaginationBean page = new PaginationBean();
			if (currPageNum == null) {
				page.setCurrentPageNum(1);
			} else {
				page.setCurrentPageNum(Integer.parseInt(currPageNum));
			}
			searchBean.setPage(page);
			List<QsRecHandoverItem> list = dao.findByFields(searchBean);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static ReturnObject removeBatch(
			HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String batchIds = request.getParameter("batchIds");
			QsRecHandoverDAO dao = new QsRecHandoverDAO();
			if (batchIds != null) {
				String[] ids = batchIds.split(";");
				for (String id : ids) {
					dao.remove(QsRecHandover.class, Integer.parseInt(id));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			ret.SetErrored(true);
			ret.setErrorMessage("删除失败！");
		}
		return ret;
	}

	public static int getPageCount(HttpServletRequest request,
			HttpServletResponse response, JspWriter out) {
		try {
			QsRecHandoverDAO dao = new QsRecHandoverDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			return (dao.count(searchBean) + PaginationBean.pageSize - 1)
					/ PaginationBean.pageSize;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 1;
	}

	private static SearchBean buildSearchCondition(HttpServletRequest request,
			HttpServletResponse response, JspWriter out) throws Exception {
		request.setCharacterEncoding("UTF-8");
		String testDate = request.getParameter("timestamp");
		String deptId = request.getParameter("deptId");
		SearchBean searchBean = new SearchBean();
		if (!StringUtil.isEmpty(deptId)) {
			FieldBean field = new FieldBean("deptId", Integer
					.parseInt(deptId));
			searchBean.add(field);
		}
		if (!StringUtil.isEmpty(testDate)) {
			Period period = PeriodUtil.getPeriod(PeriodOption.valueOf(Integer.parseInt(testDate)));
			if(!period.isAll()) {
				searchBean.setBeginTimeName("timestamp");
				searchBean.setBeginTimeValue(period.getBegin());
				searchBean.setEndTimeName("timestamp");
				searchBean.setEndTimeValue(period.getEnd());
			}
		}
		// 设置权限
		User user = (User) request.getSession().getAttribute(
				Consts.LOGINED_USER);
		if (user != null) {
			AuthorityBean authority = new AuthorityBean();
			authority.setRole(RoleType.valueOf(user.getRoleId()));
			authority.setDeptId(user.getAssociatedDept());
			searchBean.setAuthority(authority);
		}
		return searchBean;
	}
}
